package org.ww.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.glassfish.external.statistics.Statistic;
import feign.template.QueryTemplate;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.ww.statistics.bean.Daily;
import org.ww.statistics.client.MemberClient;
import org.ww.statistics.mapper.DailyMapper;
import org.ww.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-11-13
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private MemberClient memberClient;

    @Override
    public void addStatistics(String date) {
        //添加统计记录之前删除当前存在的日期
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",date);
        baseMapper.delete(wrapper);

        //调用服务查询注册人数
        Integer registerMemberCount = memberClient.getRegisterMemberCount(date);

        //添加统计记录
        Daily daily=new Daily();
        daily.setRegisterNum(registerMemberCount);
        daily.setDateCalculated(date);
        daily.setCourseNum(RandomUtils.nextInt(100,300));
        daily.setLoginNum(RandomUtils.nextInt(100,300));
        daily.setVideoViewNum(RandomUtils.nextInt(100,300));

        baseMapper.insert(daily);

    }

    /**
     * 根据开始时间和结束时间以及日期类型返回两个json数据集合
     * 日期json形式，数据json形式
     * @param type
     * @param start
     * @param end
     * @return
     */
    @Override
    public Map getShowData(String type, String start, String end) {
        // 根据条件查询相应得数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();

        wrapper.between("date_calculated",start,end);
        wrapper.select("date_calculated",type);
        List<Daily> dailies = baseMapper.selectList(wrapper);
        //创建两个集合分别存放日期和数据
        List<String> dateList=new ArrayList<>();
        List<Integer> dataList=new ArrayList<>();
        for(Daily daily:dailies){
            //将日期存进集合中
            dateList.add(daily.getDateCalculated());
            //按条件将数据存进list中
            switch (type){
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;

            }
        }
        //将两个集合存放入map中
        Map<String,Object> map=new HashMap<>();
        map.put("date_calculatedList",dateList);
        map.put("num_dataList",dataList);
        return map;
    }
}
